{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0ea03390",
   "metadata": {},
   "source": [
    "# How to customize JSON output for Reports and Test Suites"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98b0a7a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    import evidently\n",
    "except:\n",
    "    !pip install git+https://github.com/evidentlyai/evidently.git"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3546af99",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "from sklearn import datasets\n",
    "\n",
    "from evidently import ColumnMapping\n",
    "from evidently.report import Report\n",
    "from evidently.test_suite import TestSuite\n",
    "from evidently.metric_preset import DataDriftPreset\n",
    "from evidently.test_preset import DataDriftTestPreset\n",
    "from evidently.core import AllDict"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e5081f0",
   "metadata": {},
   "source": [
    "## Prepare Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e51e314",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = datasets.fetch_california_housing(as_frame=True)\n",
    "housing_data = data.frame\n",
    "\n",
    "housing_data.rename(columns={'MedHouseVal': 'target'}, inplace=True)\n",
    "housing_data['prediction'] = housing_data['target'].values + np.random.normal(0, 5, housing_data.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dffb46c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "reference = housing_data.sample(n=5000, replace=False)\n",
    "current = housing_data.sample(n=5000, replace=False)\n",
    "current.iloc[:20, 3:5] = np.nan"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34be160b",
   "metadata": {},
   "source": [
    "## Report JSON customization: default json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81788f1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "report = Report(metrics=[\n",
    "    DataDriftPreset(), \n",
    "])\n",
    "\n",
    "report.run(reference_data=reference, current_data=current)\n",
    "report.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcd8ac7a",
   "metadata": {},
   "source": [
    "## Report dict/json customization: include_render parameter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2d2bed9",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "report.json(include_render=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c3e7903",
   "metadata": {},
   "source": [
    "## Report dict/json customization: include/exclude parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e837fcd1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# specify what to include in dict/json\n",
    "\n",
    "report.as_dict(include={\n",
    "    \"DataDriftTable\": {\n",
    "        \"drift_by_columns\":{\n",
    "            \"target\":{\n",
    "                \"column_name\", \"column_type\", \"drift_score\"\n",
    "            }}}})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7289c17c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# specify what to include in dict/json, use True to include all the fields for a dictionary\n",
    "\n",
    "report.as_dict(include={\"DataDriftTable\":{\"drift_by_columns\":{\"target\":True}}})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "130283ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "# specify what to include in dict/json, use AllDict to apply filters to column-based results\n",
    "\n",
    "report.as_dict(include={\n",
    "    \"DataDriftTable\": {\n",
    "        \"drift_by_columns\":AllDict({\n",
    "                \"column_name\", \"column_type\", \"drift_score\"\n",
    "            })}},\n",
    "              exclude={\"DataDriftTable\": {\"drift_by_columns\":AllDict({\n",
    "                 \"column_type\"\n",
    "            })}})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a29f13c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# specify what to include in dict/json, combine include and exclude\n",
    "\n",
    "report.as_dict(\n",
    "    include={\"DatasetDriftMetric\": {\"share_of_drifted_columns\"}},\n",
    "    exclude={\"DataDriftTable\":{\"drift_by_columns\"}}\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77b7593c",
   "metadata": {},
   "source": [
    "## TestSuite dict/json customization: include/exclude parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddc870b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# TestSuites support include in dict/json as well\n",
    "\n",
    "test_suite = TestSuite(tests=[\n",
    "    DataDriftTestPreset(),\n",
    "])\n",
    "\n",
    "test_suite.run(reference_data=reference, current_data=current)\n",
    "test_suite.as_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "580d9e30",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "test_suite.as_dict(exclude={\"TestColumnDrift\":{\"parameters\":True}})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87663efa",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "test_suite.as_dict(\n",
    "    exclude={\"TestColumnDrift\":{\"parameters\":True}},\n",
    "    include={\"TestShareOfDriftedColumns\":{\"name\", \"status\"}}\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1664d953",
   "metadata": {},
   "source": [
    "## TestSuite dict/json customization: include_metrics and include_render parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9f52677",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# use include_metrics=True/False to include/exclude metrics data from Test Suite results \n",
    "\n",
    "test_suite.as_dict(include_metrics=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00e87e52",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# if you have included metrics in Test Suite output, you can customize what exactly to include\n",
    "# use include_render=True/False to include/exclude render-related data from metrics \n",
    "\n",
    "test_suite.as_dict(include_metrics=True, include_render=False) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95fe5b2a",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# if you have included metrics in Test Suite output, you can customize what exactly to include\n",
    "# specify what metric fields to include in the output using include/exclude parameters, that you used before for Reports\n",
    "\n",
    "test_suite.as_dict(include_metrics=True, include={\n",
    "    \"DataDriftTable\": {\n",
    "        \"drift_by_columns\":AllDict({\n",
    "                \"column_name\", \"column_type\", \"drift_score\"\n",
    "            })},\n",
    "    \"ColumnDriftMetric\": {\n",
    "        \"column_name\", \"column_type\", \"drift_score\"\n",
    "    }\n",
    "}) "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
